<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController {
	//登陆
    public function login()
    {
       if (IS_POST) {
       	$data['phone'] = I('post.phone');
       	$data['password'] = I('post.password');

       	 //手机
       	 $preg_js = "/^1[34578][0-9]{9}$/";
       	 $pass_sj = preg_match($preg_js, $data['phone']);
		 if (!$pass_sj) {
			$arr = array(
				'error' => 0,
				'info' => '请输入正确的手机号'
				);
			echo json_encode($arr);
			exit;
		 }

		 $jiazhang_user = M('jiazhang_user')->where(array('phone'=>$data['phone'],'password'=>md5($data['password'])))->find();
		 $laoshi_user = M('laoshi_user')->where(array('phone'=>$data['phone'],'password'=>md5($data['password'])))->find();

		 //判断是老师还是家长的手机
		 if ($jiazhang_user) {

			 //判断冻结，删除账号
			 if ($jiazhang_user['is_dongjie']==0 ) {
				$arr = array(
					'error' => 0,
					'info' => '账号已冻结'
					);
				echo json_encode($arr);
				exit;
			 }
			 if ($jiazhang_user['is_del']==1 ) {
				$arr = array(
					'error' => 0,
					'info' => '账号已删除'
					);
				echo json_encode($arr);
				exit;
			 }

		 	$url = U('jiazhang/jiazhang_one',array('newcity'=>$jiazhang_user['city']));
			$arr = array(
				'error' => 1,
				'info' => '欢迎会员',
				'url' => $url
				);
			echo json_encode($arr);
			
			//更新登陆时间
			$login_time = time();
			M('jiazhang_user')->where(array('phone'=>$data['phone']))->setField('login_time',$login_time);
			//登陆成功 写session
			$_SESSION["id"] = $jiazhang_user["id"];
			$_SESSION["phone"] = $jiazhang_user["phone"];
			$_SESSION["is_who"] = 'jiazhang';

		 }elseif ($laoshi_user) {

			 //判断冻结，删除账号
			 if ($laoshi_user['is_dongjie']==0 ) {
				$arr = array(
					'error' => 0,
					'info' => '账号已冻结'
					);
				echo json_encode($arr);
				exit;
			 }
			 if ($laoshi_user['is_del']==1 ) {
				$arr = array(
					'error' => 0,
					'info' => '账号已删除'
					);
				echo json_encode($arr);
				exit;
			 }
			 
		 	$url = U('laoshi/zlrz',array('newcity'=>$laoshi_user['city']));
			$arr = array(
				'error' => 1,
				'info' => '欢迎老师',
				'url' => $url
				);
			echo json_encode($arr);

			//更新登陆时间
			$login_time = time();
			M('laoshi_user')->where(array('phone'=>$data['phone']))->setField('login_time',$login_time);
			//登陆成功 写session
			$_SESSION["id"] = $laoshi_user["id"];
			$_SESSION["phone"] = $laoshi_user["phone"];
			$_SESSION["is_who"] = 'laoshi';

		 }else {
			$arr = array(
				'error' => 0,
				'info' => '账号密码错误,请确认后在输入'
				);
			echo json_encode($arr);
		 }

       }else {
        //公共显示变量
        $this->public_xianshi();
        
       	$this->display();
       }
       
    }

    //验证码
    public function vcode()
    {
    	ob_clean();
		$Verify = new \Think\Verify();
		$Verify->fontSize = 30;
		$Verify->length = 4;
		//$Verify->useNoise = false;
		$Verify->entry();
    }

	//家长注册
    public function jz_zhuce()
    { //[phone] => a [vcode] => a [password] => a [city] => 青岛
       if (IS_POST) {
       	 $data['phone'] = I('post.phone');
       	 $data['vcode'] = I('post.vcode');

		 $send_to = I('post.send_to');

       	 $data['password'] = I('post.password');
       	 $data['city'] = I('post.city');

       	 //手机
       	 $preg_js = "/^1[3458][0-9]{9}$/";
       	 $pass_sj = preg_match($preg_js, $data['phone']);
		 if (!$pass_sj) {
			$arr = array(
				'error' => 0,
				'info' => '请输入正确的手机号'
				);
			echo json_encode($arr);
			exit;
		 }
		 //存在数据库验证
		 $sql = 'select * from jiazhang_user where phone='.$data['phone'];
		 $res_phone = M()->query($sql);
		 if ($res_phone) {
			$arr = array(
				'error' => 0,
				'info' => '手机号已存在,请重新输入'
				);
			echo json_encode($arr);
			exit;
		 }
		 //存在数据库唯一验证
		 $sql = 'select * from laoshi_user where phone='.$data['phone'];
		 $laoshi_phone = M()->query($sql);
		 if ($laoshi_phone) {
			$arr = array(
				'error' => 0,
				'info' => '手机号已注册为老师账号,请更换'
				);
			echo json_encode($arr);
			exit;
		 }


		 //验证码
        $verify = new \Think\Verify(); 
        if (!$verify->check($data['vcode'])) {
			$arr = array(
				'error' => 0,
				'info' => '验证码错误'
				);
			echo json_encode($arr);
			exit;
        }

        //短信随机码验证
        if ($send_to != $_SESSION['sj_mobile']) {
			$arr = array(
				'error' => 0,
				'info' => '短信码错误'
				);
			echo json_encode($arr);
			exit;
        }

        //密码
        $preg_pass = "/^[0-9A-z]{6,16}$/";
		$pass_Yz = preg_match($preg_pass, $data['password']);
		if (!$pass_Yz) {
			$arr = array(
				'error' => 0,
				'info' => '密码必须是6-16位的字符串'
				);
			echo json_encode($arr);
			exit;
		}

		$data['password'] = md5($data['password']);
		$data['create_time'] = time();
		$data['create_ip'] =  get_client_ip();

		//入库
		$res = M('jiazhang_user')->add($data);
	       if ($res) {
	             $arr = array(
	                'error' =>1,
	                'info'=>"注册成功"
	                );
	              echo json_encode($arr);
	         }else {
		         $arr = array(
		                'error' =>0,
		                'info'=>"注册失败"
		                );
		        echo json_encode($arr);
	         }

       }else {
        //公共显示变量
        $this->public_xianshi();
       	//城市显示
       	$citys = M('city')->where('parent_id=0')->select();
       	$this->assign('citys',$citys);

       	$this->display();
       }
       
    }

	//老师注册
    public function ls_zhuce()
    {//print_r($_POST);exit;
       if (IS_POST) {
       	 $data['phone'] = I('post.phone');
       	 $data['vcode'] = I('post.vcode');

       	 $send_to = I('post.send_to');

       	 $data['password'] = I('post.password');
       	 $data['city'] = I('post.city');

       	 //手机
       	 $preg_js = "/^1[34578][0-9]{9}$/";
       	 $pass_sj = preg_match($preg_js, $data['phone']);
		 if (!$pass_sj) {
			$arr = array(
				'error' => 0,
				'info' => '请输入正确的手机号'
				);
			echo json_encode($arr);
			exit;
		 }

		 //存在数据库验证
		 $sql = 'select * from laoshi_user where phone='.$data['phone'];
		 $res_phone = M()->query($sql);
		 if ($res_phone) {
			$arr = array(
				'error' => 0,
				'info' => '手机号已存在,请重新输入'
				);
			echo json_encode($arr);
			exit;
		 }
		 //存在数据库唯一验证
		 $sql = 'select * from jiazhang_user where phone='.$data['phone'];
		 $jiazhang_phone = M()->query($sql);
		 if ($jiazhang_phone) {
			$arr = array(
				'error' => 0,
				'info' => '手机号已注册家长账号,请更换'
				);
			echo json_encode($arr);
			exit;
		 }

		 //验证码
        $verify = new \Think\Verify(); 
        if (!$verify->check($data['vcode'])) {
			$arr = array(
				'error' => 0,
				'info' => '验证码错误'
				);
			echo json_encode($arr);
			exit;
        }

        //短信随机码验证
        if ($send_to != $_SESSION['sj_mobile']) {
			$arr = array(
				'error' => 0,
				'info' => '短信码错误'
				);
			echo json_encode($arr);
			exit;
        }

        //密码
        $preg_pass = "/^[0-9A-z]{6,16}$/";
		$pass_Yz = preg_match($preg_pass, $data['password']);
		if (!$pass_Yz) {
			$arr = array(
				'error' => 0,
				'info' => '密码必须是6-16位的字符串'
				);
			echo json_encode($arr);
			exit;
		}

		$data['password'] = md5($data['password']);
		$data['create_time'] = time();
		$data['create_ip'] =  get_client_ip();

		//入库
		$res = M('laoshi_user')->add($data);
	       if ($res) {
	             $arr = array(
	                'error' =>1,
	                'info'=>"注册成功"
	                );
	              echo json_encode($arr);
	         }else {
		         $arr = array(
		                'error' =>0,
		                'info'=>"注册失败"
		                );
		        echo json_encode($arr);
	         }

       }else {
        //公共显示变量
        $this->public_xianshi();
       	//城市显示
       	$citys = M('city')->where('parent_id=0')->select();
       	$this->assign('citys',$citys);
       	
       	$this->display();
       }
    }

    //退出登陆
    public function logout()
    {
		session_unset();
		session_destroy();
		$this->redirect('index/index');
    }


    //重置密码 显示
    public function zhao_password()
    {
    	$this->display();
    }
    //重置密码
    public function zhao_password_re()
    {// [sf] => 会员 [phone] => 13430372763 [vcode] => 1 [send_to] => 1 [password] => 1
    	if (IS_POST) {
    		$sf = I('post.sf');
    		$phone = I('post.phone');
    		$vcode = I('post.vcode');
    		$send_to = I('post.send_to');
    		$password = I('post.password');

	       	 //手机
	       	 $preg_js = "/^1[34578][0-9]{9}$/";
	       	 $pass_sj = preg_match($preg_js, $phone);
			 if (!$pass_sj) {
				$arr = array(
					'error' => 0,
					'info' => '请输入正确的手机号'
					);
				echo json_encode($arr);
				exit;
			 }
			 //验证码
	        $verify = new \Think\Verify(); 
	        if (!$verify->check($vcode)) {
				$arr = array(
					'error' => 0,
					'info' => '验证码错误'
					);
				echo json_encode($arr);
				exit;
	        }
	        //短信随机码验证
	        if ($send_to != $_SESSION['sj_mobile']) {
				$arr = array(
					'error' => 0,
					'info' => '短信码错误'
					);
				echo json_encode($arr);
				exit;
	        }
	        //密码
	        $preg_pass = "/^[0-9A-z]{6,16}$/";
			$pass_Yz = preg_match($preg_pass, $password);
			if (!$pass_Yz) {
				$arr = array(
					'error' => 0,
					'info' => '密码必须是6-16位的字符串'
					);
				echo json_encode($arr);
				exit;
			}
    		//是老师 或 家长
    		if ($sf == '老师') {
    			$res = M('laoshi_user')->where(array('phone'=>$phone))->setField('password',md5($password));
    			if ($res) {
					$arr = array(
						'error' => 1,
						'info' => '重置成功'
						);
					echo json_encode($arr);
    			}else {
					$arr = array(
						'error' => 0,
						'info' => '重置失败'
						);
					echo json_encode($arr);
    			}
    		}elseif ($sf == '会员') {
    			$res = M('jiazhang_user')->where(array('phone'=>$phone))->setField('password',md5($password));
    			if ($res) {
					$arr = array(
						'error' => 1,
						'info' => '重置成功'
						);
					echo json_encode($arr);
    			}else {
					$arr = array(
						'error' => 0,
						'info' => '重置失败'
						);
					echo json_encode($arr);
    			}
    		}
    	}
    }


	//秒滴短信发送开始----------------------
    public function send_to()
    {
    	if (IS_POST) {
    		$to = I('post.to');

	       	 //手机
	       	 $preg_js = "/^1[34578][0-9]{9}$/";
	       	 $pass_sj = preg_match($preg_js, $to);
			 if (!$pass_sj) {
				$arr = array(
					'error' => 0,
					'info' => '请输入正确的手机号'
					);
				echo json_encode($arr);
				exit;
			 }

			 //发送
			miaodi($to);
			$arr = array(
				'error' => 1,
				'info' => '正在发送...',
				);
			echo json_encode($arr);
			 
    	}
    }



}